<?php

/**
 * SocialEngine
 *
 * @category   Application_Extensions
 * @package    Sitepagealbum
 * @copyright  Copyright 2010-2011 BigStep Technologies Pvt. Ltd.
 * @license    http://www.socialengineaddons.com/license/
 * @version    $Id: install.php 2011-05-05 9:40:21Z SocialEngineAddOns $
 * @author     SocialEngineAddOns
 */
class Sitepagealbum_Installer extends Engine_Package_Installer_Module {
	
	function onPreInstall() {
		
		$db = $this->getDb ();
		
		//CHECK THAT SITEPAGE PLUGIN IS ACTIVATED OR NOT
		$select = new Zend_Db_Select ( $db );
		$select->from ( 'engine4_core_settings' )->where ( 'name = ?', 'sitepage.is.active' )->limit ( 1 );
		$sitepage_settings = $select->query ()->fetchAll ();
		if (! empty ( $sitepage_settings )) {
			$sitepage_is_active = $sitepage_settings [0] ['value'];
		} else {
			$sitepage_is_active = 0;
		}
		
		//CHECK THAT SITEPAGE PLUGIN IS INSTALLED OR NOT
		$select = new Zend_Db_Select ( $db );
		$select->from ( 'engine4_core_modules' )->where ( 'name = ?', 'sitepage' )->where ( 'enabled = ?', 1 );
		$check_sitepage = $select->query ()->fetchObject ();
		if (! empty ( $check_sitepage ) && ! empty ( $sitepage_is_active )) {
			$PRODUCT_TYPE = 'sitepagealbum';
			$PLUGIN_TITLE = 'Sitepagealbum';
			$PLUGIN_VERSION = '4.1.8';
			$PLUGIN_CATEGORY = 'plugin';
			$PRODUCT_DESCRIPTION = 'Sitepagealbum Plugin';
			$PRODUCT_TITLE = 'Directory / Pages - Albums Extension';
			$_PRODUCT_FINAL_FILE = 0;
			$SocialEngineAddOns_version = '4.1.8';
			$file_path = APPLICATION_PATH . "/application/modules/$PLUGIN_TITLE/controllers/license/ilicense.php";
			$is_file = file_exists ( $file_path );
			if (empty ( $is_file )) {
				include APPLICATION_PATH . "/application/modules/Sitepage/controllers/license/license4.php";
			} else {
				include $file_path;
			}
			
			// ============ Querys Start for "Suggestion Plugin". ================
			// Insert when "Page album Plugin Enabled".
			$select = new Zend_Db_Select ( $db );
			$select->from ( 'engine4_core_modules' )->where ( 'name = ?', 'suggestion' );
			$is_enabled = $select->query ()->fetchObject ();
			if (! empty ( $is_enabled )) {
				// Notification  Type: Insert in data base.
				$db->query ( 'INSERT IGNORE INTO `engine4_activity_notificationtypes` (`type`, `module`, `body`, `is_request`, `handler`) VALUES
("page_album_suggestion", "suggestion", \'{item:$subject} has suggested to you a {item:$object:page album}.\', 1, "suggestion.widget.get-notify")' );
				
				// Insert In Mailteplates : 
				$db->query ( 'INSERT IGNORE INTO  `engine4_core_mailtemplates` ( `type` , `module` , `vars` ) VALUES ("notify_page_album_suggestion",  "suggestion",  "[suggestion_sender], [suggestion_entity], [email], [link]")' );
				
				// Insert into "core_settings" table.
				$db->query ( 'INSERT IGNORE INTO `engine4_core_settings` (`name`, `value`) VALUES ("sitepage.albumsugg.link", 1)' );
			}
			
			// ============ Querys End for "Suggestion Plugin". ==================
			//CODE FOR INCREASE THE SIZE OF engine4_authorization_permissions's FIELD type
			$type_array = $db->query ( "SHOW COLUMNS FROM engine4_authorization_permissions LIKE 'type'" )->fetch ();
			if (! empty ( $type_array )) {
				$varchar = $type_array ['Type'];
				$length_varchar = explode ( "(", $varchar );
				$length = explode ( ")", $length_varchar [1] );
				$length_type = $length [0];
				if ($length_type < 32) {
					$run_query = $db->query ( "ALTER TABLE `engine4_authorization_permissions` CHANGE `type` `type` VARCHAR( 32 ) CHARACTER SET latin1 COLLATE latin1_general_ci NOT NULL" );
				}
			}
			
			//CODE FOR INCREASE THE SIZE OF engine4_authorization_allow's FIELD type
			$type_array = $db->query ( "SHOW COLUMNS FROM engine4_authorization_allow LIKE 'resource_type'" )->fetch ();
			if (! empty ( $type_array )) {
				$varchar = $type_array ['Type'];
				$length_varchar = explode ( "(", $varchar );
				$length = explode ( ")", $length_varchar [1] );
				$length_type = $length [0];
				if ($length_type < 32) {
					$run_query = $db->query ( "ALTER TABLE `engine4_authorization_allow` CHANGE `resource_type` `resource_type` VARCHAR( 32 ) CHARACTER SET latin1 COLLATE latin1_general_ci NOT NULL" );
				}
			}
			
			//CODE FOR INCREASE THE SIZE OF engine4_activity_attachments's FIELD type
			$type_array = $db->query ( "SHOW COLUMNS FROM engine4_activity_attachments LIKE 'type'" )->fetch ();
			if (! empty ( $type_array )) {
				$varchar = $type_array ['Type'];
				$length_varchar = explode ( "(", $varchar );
				$length = explode ( ")", $length_varchar [1] );
				$length_type = $length [0];
				if ($length_type < 32) {
					$run_query = $db->query ( "ALTER TABLE `engine4_activity_attachments` CHANGE `type` `type` VARCHAR( 32 ) CHARACTER SET latin1 COLLATE latin1_general_ci NOT NULL" );
				}
			}
			
			//CODE FOR INCREASE THE SIZE OF engine4_activity_notifications's FIELD type
			$type_array = $db->query ( "SHOW COLUMNS FROM engine4_activity_notifications LIKE 'subject_type'" )->fetch ();
			if (! empty ( $type_array )) {
				$varchar = $type_array ['Type'];
				$length_varchar = explode ( "(", $varchar );
				$length = explode ( ")", $length_varchar [1] );
				$length_type = $length [0];
				if ($length_type < 32) {
					$run_query = $db->query ( "ALTER TABLE `engine4_activity_notifications` CHANGE `subject_type` `subject_type` VARCHAR( 32 ) CHARACTER SET latin1 COLLATE latin1_general_ci NOT NULL" );
				}
			}
			
			//CODE FOR INCREASE THE SIZE OF engine4_activity_notifications's FIELD type
			$type_array = $db->query ( "SHOW COLUMNS FROM engine4_activity_notifications LIKE 'object_type'" )->fetch ();
			if (! empty ( $type_array )) {
				$varchar = $type_array ['Type'];
				$length_varchar = explode ( "(", $varchar );
				$length = explode ( ")", $length_varchar [1] );
				$length_type = $length [0];
				if ($length_type < 32) {
					$run_query = $db->query ( "ALTER TABLE `engine4_activity_notifications` CHANGE `object_type` `object_type` VARCHAR( 32 ) CHARACTER SET latin1 COLLATE latin1_general_ci NOT NULL" );
				}
			}
			$select = new Zend_Db_Select ( $db );
			$select->from ( 'engine4_core_modules' )->where ( 'name = ?', 'sitepagealbum' )->where ( 'version <= ?', '4.1.5p1' );
			$is_enabled = $select->query ()->fetchObject ();
			if (! empty ( $is_enabled )) {
				$select = new Zend_Db_Select ( $db );
				$select_page = $select->from ( 'engine4_core_pages', 'page_id' )->where ( 'name = ?', 'sitepage_index_view' )->limit ( 1 );
				$page = $select_page->query ()->fetchAll ();
				if (! empty ( $page )) {
					$page_id = $page [0] ['page_id'];
					//INSERT THE PAGE PROFILE PHOTO WIDGET IN ADMIN CONTENT TABLE    
					$select = new Zend_Db_Select ( $db );
					$select_content = $select->from ( 'engine4_sitepage_admincontent' )->where ( 'page_id = ?', $page_id )->where ( 'type = ?', 'widget' )->where ( 'name = ?', 'sitepage.photos-sitepage' )->limit ( 1 );
					$content = $select_content->query ()->fetchAll ();
					if (empty ( $content )) {
						$select = new Zend_Db_Select ( $db );
						$select_container = $select->from ( 'engine4_sitepage_admincontent', 'admincontent_id' )->where ( 'page_id = ?', $page_id )->where ( 'type = ?', 'container' )->limit ( 1 );
						$container = $select_container->query ()->fetchAll ();
						if (! empty ( $container )) {
							$container_id = $container [0] ['admincontent_id'];
							$select = new Zend_Db_Select ( $db );
							$select_middle = $select->from ( 'engine4_sitepage_admincontent' )->where ( 'parent_content_id = ?', $container_id )->where ( 'type = ?', 'container' )->where ( 'name = ?', 'middle' )->limit ( 1 );
							$middle = $select_middle->query ()->fetchAll ();
							if (! empty ( $middle )) {
								$middle_id = $middle [0] ['admincontent_id'];
								$select = new Zend_Db_Select ( $db );
								$select_tab = $select->from ( 'engine4_sitepage_admincontent' )->where ( 'type = ?', 'widget' )->where ( 'name = ?', 'core.container-tabs' )->where ( 'page_id = ?', $page_id )->limit ( 1 );
								$tab = $select_tab->query ()->fetchAll ();
								$tab_id = 0;
								if (! empty ( $tab )) {
									$tab_id = $tab [0] ['admincontent_id'];
								} else {
									$tab_id = $middle_id;
								}
								
								$db->insert ( 'engine4_sitepage_admincontent', array ('page_id' => $page_id, 'type' => 'widget', 'name' => 'sitepage.photos-sitepage', 'parent_content_id' => $tab_id, 'order' => 110, 'params' => '{"title":"Photos","titleCount":"true"}' ) );
							}
						}
					}
				}
				
				//INSERT THE RANDOM ALBUM WIDGET IN ADMIN CONTENT TABLE
				$select = new Zend_Db_Select ( $db );
				$select_content = $select->from ( 'engine4_sitepage_admincontent' )->where ( 'page_id = ?', $page_id )->where ( 'type = ?', 'widget' )->where ( 'name = ?', 'sitepage.albums-sitepage' )->limit ( 1 );
				$content = $select_content->query ()->fetchAll ();
				if (empty ( $content )) {
					$select = new Zend_Db_Select ( $db );
					$select_container = $select->from ( 'engine4_sitepage_admincontent', 'admincontent_id' )->where ( 'page_id = ?', $page_id )->where ( 'type = ?', 'container' )->limit ( 1 );
					$container = $select_container->query ()->fetchAll ();
					if (! empty ( $container )) {
						$container_id = $container [0] ['admincontent_id'];
						$select = new Zend_Db_Select ( $db );
						$select_left = $select->from ( 'engine4_sitepage_admincontent' )->where ( 'parent_content_id = ?', $container_id )->where ( 'type = ?', 'container' )->where ( 'name = ?', 'left' )->limit ( 1 );
						$left = $select_left->query ()->fetchAll ();
						if (! empty ( $left )) {
							$left_id = $left [0] ['admincontent_id'];
							$db->insert ( 'engine4_sitepage_admincontent', array ('page_id' => $page_id, 'type' => 'widget', 'name' => 'sitepage.albums-sitepage', 'parent_content_id' => $left_id, 'order' => 25, 'params' => '{"title":"Albums","titleCount":""}' ) );
						}
					}
				}
				
				//INSERT THE PHOTO STRIP WIDGET IN ADMIN CONTENT TABLE 
				$select = new Zend_Db_Select ( $db );
				$select_content = $select->from ( 'engine4_sitepage_admincontent' )->where ( 'page_id = ?', $page_id )->where ( 'type = ?', 'widget' )->where ( 'name = ?', 'sitepage.photorecent-sitepage' )->limit ( 1 );
				$content = $select_content->query ()->fetchAll ();
				if (empty ( $content )) {
					$select = new Zend_Db_Select ( $db );
					$select_container = $select->from ( 'engine4_sitepage_admincontent', 'admincontent_id' )->where ( 'page_id = ?', $page_id )->where ( 'type = ?', 'container' )->limit ( 1 );
					$container = $select_container->query ()->fetchAll ();
					if (! empty ( $container )) {
						$container_id = $container [0] ['admincontent_id'];
						$select = new Zend_Db_Select ( $db );
						$select_middle = $select->from ( 'engine4_sitepage_admincontent' )->where ( 'parent_content_id = ?', $container_id )->where ( 'type = ?', 'container' )->where ( 'name = ?', 'middle' )->limit ( 1 );
						$middle = $select_middle->query ()->fetchAll ();
						if (! empty ( $middle )) {
							$middle_id = $middle [0] ['admincontent_id'];
							$db->insert ( 'engine4_sitepage_admincontent', array ('page_id' => $page_id, 'type' => 'widget', 'name' => 'sitepage.photorecent-sitepage', 'parent_content_id' => $middle_id, 'order' => 5, 'params' => '{"title":"","titleCount":""}' ) );
						}
					}
				}
			}
			parent::onPreInstall ();
		} elseif (! empty ( $check_sitepage ) && empty ( $sitepage_is_active )) {
			$baseUrl = $_SERVER ['HTTP_HOST'] . Zend_Controller_Front::getInstance ()->getBaseUrl ();
			$url_string = Zend_Controller_Front::getInstance ()->getRequest ()->getRequestUri ();
			if (strstr ( $url_string, "manage/install" )) {
				$calling_from = 'install';
			} else if (strstr ( $url_string, "manage/query" )) {
				$calling_from = 'queary';
			}
			$explode_base_url = explode ( "/", $baseUrl );
			foreach ( $explode_base_url as $url_key ) {
				if ($url_key != 'install') {
					$core_final_url .= $url_key . '/';
				}
			}
			
			return $this->_error ( "<span style='color:red'>Note: You have installed the Page Plugin but not activated it on your site yet. Please activate it first before installing the Page Album Plugin.</span><br/> <a href='" . 'http://' . $core_final_url . "admin/sitepage/settings/readme'>Click here</a> to activate the Page Plugin." );
		} else {
			$base_url = Zend_Controller_Front::getInstance ()->getBaseUrl ();
			return $this->_error ( "<span style='color:red'>Note: You have not installed the Page Plugin on your site yet. Please install it first before installing the Page Album Plugin.</span><br/> <a href='" . $base_url . "/manage'>Click here</a> to go Manage Packages." );
		}
	}
}

?>